{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python390jvsc74a57bd063fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d",
   "display_name": "Python 3.9.0 64-bit"
  },
  "metadata": {
   "interpreter": {
    "hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.common.exceptions import TimeoutException, NoSuchElementException\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "import time\n",
    "import requests\n",
    "from hashlib import md5\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver import ChromeOptions\n",
    "options = ChromeOptions()     #实例化一个ChromeOptions对象\n",
    "options.add_argument(\"start-maximized\")\n",
    "options.add_experimental_option('excludeSwitches', ['enable-automation'])  #以键值对的形式加入参数\n",
    "options.add_experimental_option('useAutomationExtension', False)\n",
    "option.add_argument(\"--disable-blink-features\")\n",
    "option.add_argument(\"--disable-blink-features=AutomationControlled\") # 屏蔽webdriver特征\n",
    "browser = webdriver.Chrome(options=options)\n",
    "wait = WebDriverWait(browser, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "\n",
    "# npx extract-stealth-evasions\n",
    "# everything stealth.min.js\n",
    "with open('./stealth.min.js') as f:\n",
    "    js = f.read()\n",
    "    print(js)\n",
    "browser.execute_cdp_cmd(\"Page.addScriptToEvaluateOnNewDocument\", {\n",
    "    \"source\": js\n",
    "})\n",
    "# window.navigator.webdriver\n",
    "browser.execute_cdp_cmd(\"Page.addScriptToEvaluateOnNewDocument\", {\n",
    "  \"source\": \"\"\"\n",
    "    Object.defineProperty(navigator, 'webdriver', {\n",
    "      get: () => false\n",
    "    })\n",
    "  \"\"\"\n",
    "})\n",
    "\n",
    "# 检验\n",
    "browser.get('https://bot.sannysoft.com/')\n",
    "time.sleep(10)\n",
    "browser.save_screenshot('walkaround.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# window.navigator.webdriver\n",
    "browser.execute_cdp_cmd(\"Page.addScriptToEvaluateOnNewDocument\", {\n",
    "  \"source\": \"\"\"\n",
    "    Object.defineProperty(navigator, 'webdriver', {\n",
    "      get: () => false\n",
    "    })\n",
    "  \"\"\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url=\"https://www.toutiao.com/c/user/token/MS4wLjABAAAAir5m0x4XgaNHpQJa6oD8VDuhNz1xwI72EzHUgdYpwMqDhXBK8d7Y4Ru8qTrgug67/\"\n",
    "browser.get(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}